package com.rongshu.view.penpal.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.home.activity.SingleArticleDetailsActivity;
import com.rongshu.view.penpal.activity.PostDetailsActivity;
import com.rongshu.view.penpal.bean.PenpalArticleResponse;
import com.rongshu.view.penpal.view.ArticleAdapterListener;
import com.rongshu.widget.BanyanDialog;
import com.widget.SwipeMenuView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 * description：笔友会 文章/帖子管理列表
 */
public class PenpalArticleListManagerAdapter extends RecyclerView.Adapter<PenpalArticleListManagerAdapter.ViewHolder> {
    private Context context;
    private List<PenpalArticleResponse.ResponseBean> list;
    private ArticleAdapterListener listener;
    private int type;

    public PenpalArticleListManagerAdapter(Context context, List<PenpalArticleResponse.ResponseBean> list, int type) {
        this.list = list;
        this.context = context;
        this.type = type;
    }

    public void setAdapter(ArticleAdapterListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_article_item_swipemenu, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        viewHolder.postCover.setVisibility(TextUtils.isEmpty(list.get(i).getTopImg()) ? View.GONE : View.VISIBLE);
        GlideUtils.loadImageView(context, viewHolder.postCover, list.get(i).getTopImg(),
                R.drawable.default_preview_icon, R.drawable.default_preview_icon);
        viewHolder.name.setText(list.get(i).getBi_name());
        viewHolder.title.setVisibility(TextUtils.isEmpty(list.get(i).getTitle()) ? View.GONE : View.VISIBLE);
        viewHolder.title.setText(list.get(i).getTitle());
        try {
            String content = list.get(i).getContents_header().replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            viewHolder.content.setText(URLDecoder.decode(content, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        GlideUtils.loadImageView(context, viewHolder.logo, list.get(i).getAvatar(),
                R.drawable.user_head_icon, R.drawable.user_head_icon);
        viewHolder.praiseNum.setText("点赞 " + list.get(i).getLikeCount());
        viewHolder.commentNum.setText("评论 " + list.get(i).getCommentCount());
        viewHolder.time.setText(DateUtils.ConvertDate(list.get(i).getCreate_datetime()));
        viewHolder.vip.setVisibility(list.get(i).getVip() ? View.VISIBLE : View.GONE);
        if (list.get(i).getIstop() == 0) {
            viewHolder.firstBtn.setText("置顶");
            viewHolder.status.setVisibility(View.GONE);
            ViewGroup.LayoutParams layoutParams = viewHolder.firstBtn.getLayoutParams();
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.width = (int) context.getResources().getDimension(R.dimen.dd_dimen_140px);
            viewHolder.firstBtn.setLayoutParams(layoutParams);
        } else {
            ViewGroup.LayoutParams layoutParams = viewHolder.firstBtn.getLayoutParams();
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.width = (int) context.getResources().getDimension(R.dimen.dd_dimen_180px);
            viewHolder.firstBtn.setLayoutParams(layoutParams);

            viewHolder.firstBtn.setText("取消置顶");
            viewHolder.status.setVisibility(View.VISIBLE);
            viewHolder.status.setBackgroundResource(R.drawable.top_icon);
        }
        if (TextUtils.equals(list.get(i).getTopic_type(), "transpond")) {
            viewHolder.flow.setVisibility(View.VISIBLE);
        } else {
            viewHolder.flow.setVisibility(View.GONE);
        }
        if (list.get(i).getIsPlaza() == 0) {
            viewHolder.thridBtn.setText("推到广场");
        } else {
            viewHolder.thridBtn.setText("撤出广场");
        }
        if (list.get(i).getRank() == 1) {
            viewHolder.level.setText("会长");
            viewHolder.level.setVisibility(View.VISIBLE);
        } else {
            viewHolder.level.setVisibility(View.GONE);
        }
//        viewHolder.thridBtn.setVisibility(type == Constant.ARTICLE ? View.GONE : View.VISIBLE);
        viewHolder.firstBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onSetTopListener(i);
                }
                viewHolder.swipeMenuView.quickClose();
            }
        });
        viewHolder.secondBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    String msg = type == Constant.ARTICLE ? "确定要删除文章吗" : "确定要删除帖子吗";
                    BanyanDialog.Builder builder = new BanyanDialog.Builder(context);
                    builder.setTitle("提示");
                    builder.setContent(msg);
                    builder.setLeft("取消");
                    builder.setRight("确定");
                    final BanyanDialog dialog = builder.create();
                    builder.setDialogOnClick(new BanyanDialog.Builder.onClickListener() {
                        @Override
                        public void leftClickListener() {
                            dialog.dismiss();
                        }

                        @Override
                        public void rightClickListener() {
                            listener.onDeleteListener(i);
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                }
                viewHolder.swipeMenuView.quickClose();
            }
        });
        viewHolder.thridBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.pushPiazza(i);
                }
                viewHolder.swipeMenuView.quickClose();
            }
        });
        viewHolder.main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null){
                    listener.onItemClickListener(i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView title, content, name, commentNum, praiseNum, time, firstBtn, secondBtn, thridBtn, level;
        private ImageView postCover, logo, status, vip, flow;
        private SwipeMenuView swipeMenuView;
        private LinearLayout main;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            postCover = itemView.findViewById(R.id.post_cover);
            logo = itemView.findViewById(R.id.author_logo);
            name = itemView.findViewById(R.id.author_name);
            level = itemView.findViewById(R.id.author_level);
            time = itemView.findViewById(R.id.wirte_time);
            title = itemView.findViewById(R.id.post_title);
            content = itemView.findViewById(R.id.post_content);
            flow = itemView.findViewById(R.id.share_flag);
            status = itemView.findViewById(R.id.post_label);
            praiseNum = itemView.findViewById(R.id.praise_num);
            commentNum = itemView.findViewById(R.id.comment_num);
            vip = itemView.findViewById(R.id.vip);

            main = itemView.findViewById(R.id.main_layout);
            swipeMenuView = itemView.findViewById(R.id.swipemenu_view);
            firstBtn = itemView.findViewById(R.id.btn_first);
            secondBtn = itemView.findViewById(R.id.btn_second);
            thridBtn = itemView.findViewById(R.id.btn_third);
        }
    }
}
